package me.skuli.NetherWorld.Plugins;

import java.util.Random;

import me.skuli.NetherWorld.WorldGenerator;
import me.skuli.NetherWorld.Plats.PlatLot;
import me.skuli.NetherWorld.Support.CachedYs;
import me.skuli.NetherWorld.Support.RealChunk;
import me.skuli.NetherWorld.Support.TekkitMaterial;

import org.bukkit.block.Block;

public class OreProvider_Tekkit extends OreProvider {

	public OreProvider_Tekkit(WorldGenerator generator) {
		super(generator);
		
	}

	/**
	 * Populates the world with ores.
	 *
	 * @author Nightgunner5
	 * @author Markus Persson
	 * modified by simplex
	 * wildly modified by daddychurchill
	 * tekkit support by gunre
	 */
	
	private static final int[] ore_types = new int[] {TekkitMaterial.NETHER_COAL,
												      TekkitMaterial.NETHER_DIAMOND,
													  TekkitMaterial.NETHER_GOLD,
													  TekkitMaterial.NETHER_IRON,
													  TekkitMaterial.NETHER_LAPIS,
													  TekkitMaterial.NETHER_REDSTONE,
													  TekkitMaterial.NETHER_COPPER,
													  TekkitMaterial.NETHER_TIN
													  };
	
	//                                                   	        COAL   IRON   GOLD  LAPIS  REDST   DIAM    TIN  COPPR   TUNG   NIKO   MARB   BASA   URAN  
	private static final int[] ore_iterations = new int[]    {    15,     6,     2,     2,     4,     1,     6,     6};
	private static final int[] ore_amountToDo = new int[]    {     8,     6,     3,     2,     4,     2,     6,     6};
	private static final int[] ore_maxY = new int[]          {   128,    68,    34,    30,    17,    16,    68,    96};
	private static final int[] ore_minY = new int[]          {     1,     1,     1,     1,     1,     1,     1,     1};
	private static final boolean[] ore_upper = new boolean[] {  true, true,  true,  true,  true,  true,  true, true};
	private static final boolean[] ore_physics = new boolean[] {false,  false, false, false, false, false, false, false};
	private static final boolean[] ore_liquid = new boolean[] { false,  false, false, false, false, false, false, false};

	@Override
	public void sprinkleOres(WorldGenerator generator, PlatLot lot, RealChunk chunk, CachedYs blockYs, Random random, OreLocation location) {
		for (int typeNdx = 0; typeNdx < ore_types.length; typeNdx++) {
			sprinkleOre(generator, lot, chunk, blockYs,
					random, ore_types[typeNdx], ore_maxY[typeNdx], 
					ore_minY[typeNdx], ore_iterations[typeNdx], 
					ore_amountToDo[typeNdx], ore_upper[typeNdx], ore_physics[typeNdx], ore_liquid[typeNdx]);
		}
	}

	@Override
	protected boolean placeBlock(RealChunk chunk, Random random, int x, int y, int z, int typeId, boolean physics) {
		if (random.nextDouble() < oreSprinkleOdds) {
			Block block = chunk.getActualBlock(x, y, z);
			if (block.getTypeId() == stratumId) {
				
				//TODO I like this idea, generalize it!
				if (typeId > TekkitMaterial.typeScale) {
					int blockId = typeId / TekkitMaterial.typeScale;
					byte dataVal = (byte) (typeId % TekkitMaterial.typeScale);
					block.setTypeIdAndData(blockId, dataVal, physics);
				} else {
					block.setTypeId(typeId, physics);
				}
				return true;
			}
		}
		return false;
	}
}
